//! Functions for creating and handling ngrams.

/// Given a word and n, returns a vector of strings consisting 
/// of ngrams of characters from the word.
///
/// # Examples
///
/// ```
/// assert_eq!(gecliht::word_ngrams("", 2), vec![""]);
/// assert_eq!(gecliht::word_ngrams("ABCDE", 2), vec!["AB", "BC", "CD", "DE"]);
/// assert_eq!(gecliht::word_ngrams("ABCDE", 4), vec!["ABCD", "BCDE"]);
/// ```
///
pub fn word_ngrams (word: &str, n: usize) -> Vec<String> {
    if n >= word.len() {
        return vec![word.to_string()];
    }

    (0..=(word.len()-n))
        .map(|i| word[i..(i+n)].to_string())
        .collect()
}

#[cfg(test)]
     mod tests {
         use super::*;

         #[test]
         fn test_word_ngrams () {
             assert_eq!(vec![""],
                        word_ngrams("", 2));
             assert_eq!(vec!["A", "B", "C", "D", "E"],
                        word_ngrams("ABCDE", 1));
             assert_eq!(vec!["AB", "BC", "CD", "DE"],
                        word_ngrams("ABCDE", 2));
             assert_eq!(vec!["ABC", "BCD", "CDE"],
                        word_ngrams("ABCDE", 3));
             assert_eq!(vec!["ABCD", "BCDE"],
                        word_ngrams("ABCDE", 4));
             assert_eq!(vec!["ABCDE"],
                        word_ngrams("ABCDE", 5));
             assert_eq!(vec!["ABCDE"],
                        word_ngrams("ABCDE", 5));
         }
     }
